% Function to write 32-bit entries into a binary file from a 2D array of
% size [nr, nc]
function array2bin(binfile, img, nr, nc, q);
     
img_q = quantize(q, img);
        %convert to column vector (row-major)
        img_1d = reshape(img_q', nr*nc, 1);
        
            % convert to hex
        img_1d_hex = num2hex(q, img_1d);
        img_1d_hex2dec= hex2dec(img_1d_hex);
        img_1d_hex2dec=uint32(img_1d_hex2dec);
        
             % Write into the file
        fid = fopen(binfile, 'w+');
        for j=1:size(img_1d_hex2dec,1)
            % img_1d_hex2dec(j,1) =  1431677610;% 5555AAAA    % 1431655765 55555555;
         %   fprintf(fid, '%X\n',img_1d(j,:));
         % fprintf(fid,'%8s\n', img_1d_hex(j,:));
           fwrite(fid,img_1d_hex2dec(j,1),'uint32'); %'b'additional parameter reverses bit order;
        end
        fclose(fid);